package com.starsea.book.ui.newpage.adapter

import android.content.Context
import android.view.ViewGroup
import com.starsea.book.base.adapter.ItemViewHolder
import com.starsea.book.base.adapter.RecyclerAdapter
import com.starsea.book.databinding.ItemMineReadHistoryBinding
import com.starsea.book.ui.newpage.entity.ReadHistoryEntity
import com.starsea.book.ui.newpage.utils.loadNormalImage
import com.starsea.book.ui.widget.recycler.ItemTouchCallback

class ReadHistoryAdapter(
    context: Context,
    var onChanged: ((value: ReadHistoryEntity.List, type: Int, position: Int) -> Unit)? = null
) :
    RecyclerAdapter<ReadHistoryEntity.List, ItemMineReadHistoryBinding>(context),
    ItemTouchCallback.Callback {


    override fun getViewBinding(parent: ViewGroup): ItemMineReadHistoryBinding {
        return ItemMineReadHistoryBinding.inflate(inflater, parent, false)
    }

    override fun registerListener(holder: ItemViewHolder, binding: ItemMineReadHistoryBinding) {
        binding.rtvAdd.setOnClickListener {
            getItem(holder.layoutPosition)?.let {
                onChanged!!.invoke(it, 1, holder.layoutPosition)
            }
        }
        binding.root.setOnClickListener {
            getItem(holder.layoutPosition)?.let {
                onChanged!!.invoke(it, 0, holder.layoutPosition)
            }
        }
        binding.rtvDelete.setOnClickListener {
            getItem(holder.layoutPosition)?.let {
                onChanged!!.invoke(it, 2, holder.layoutPosition)
            }
        }
        binding.swParent.setOnClickListener {
            getItem(holder.layoutPosition)?.let {
                onChanged!!.invoke(it, 0, holder.layoutPosition)
            }
        }
        binding.clParent.setOnClickListener {
            getItem(holder.layoutPosition)?.let {
                onChanged!!.invoke(it, 0, holder.layoutPosition)
            }
        }
    }

    override fun convert(
        holder: ItemViewHolder,
        binding: ItemMineReadHistoryBinding,
        item: ReadHistoryEntity.List,
        payloads: MutableList<Any>
    ) {
        binding.run {
            ivBookCover.loadNormalImage(context, item.bookCover ?: "")
            tvBookName.text = item.bookTitle
            tvChapter.text = item.sectionTitle
            tvReadTime.text = item.readTime
//                0:未在书架;1:已在书架
            rtvAdd.text = if (item.shelfStatus == 1) "打开" else "加入书架"


        }
    }
}